package com.example.zengyan.myphonebook;

import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Window;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.beardedhen.androidbootstrap.BootstrapButton;
import com.beardedhen.androidbootstrap.BootstrapEditText;

import BD.HttpUtil;
import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class LoginActivity extends AppCompatActivity {

    private SharedPreferences pref;
    SharedPreferences.Editor editor;
    @Bind(R.id.checkbox_id)
    CheckBox checkBox;

    @Bind(R.id.stunumEt_id)
    BootstrapEditText stunumEt;

    @Bind(R.id.passwordEt_id)
    BootstrapEditText passwordEt;

    @OnClick(R.id.LoginBtn_id)
    public void tologin(){
            login();
    }

    private void login(){
        final String stunum=stunumEt.getText().toString().trim();
        final String password=passwordEt.getText().toString().trim();
        if (stunum.equals("")||password.equals("")){
            Toast.makeText(LoginActivity.this, "请输入登录信息", Toast.LENGTH_SHORT).show();
            return;
        }


        final ProgressDialog pd1 = ProgressDialog.show(LoginActivity.this, "正在登陆。。。", "等一下哦。。。");
        pd1.setCancelable(true);
        HttpUtil.login(LoginActivity.this, stunum, password, new HttpUtil.Callback() {
            @Override
            public void onSuccess(String result) {

                if (checkBox.isChecked()) {
                    editor.putBoolean("remember", true);
                    editor.putString("stunum", stunum);
                    editor.putString("password", password);
                } else {
                    editor.clear();
                }

                editor.commit();
                pd1.dismiss();
                LoginActivity.this.finish();
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                Toast.makeText(LoginActivity.this, "登陆成功", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFail() {
                pd1.dismiss();
                Toast.makeText(LoginActivity.this, "登陆失败", Toast.LENGTH_SHORT).show();
            }
        });


    }



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);

        ButterKnife.bind(this);
        pref= PreferenceManager.getDefaultSharedPreferences(this);
        editor=pref.edit();
        if (pref.getBoolean("remember", false)) {
            String stunum=pref.getString("stunum","");
            String password=pref.getString("password","");
            stunumEt.setText(stunum);
            passwordEt.setText(password);
            checkBox.setChecked(true);
            Intent intent=getIntent();
            if (!intent.getBooleanExtra("leave", false)) {
                login();
            }

        }

    }
}
